﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LogisticsManage : MonoBehaviour {

    private List<Vector3> gAGVTrack;
   // private List<Vector3> gMeterailTrack;
    private AGVBase gAGVBase;
    private MachineBase gMachineBase1;
    private MachineBase gMachineBase2;
    private MachineBase gMachineBase3;
    private MachineBase gMachineBase4;
    private Transform gAGVTransform;

    private OutPutMaterialManage gFinishMaterailManage;
    private InPutMaterialManage gOriginMaterialManage;

    private GameObject gPreCubes;//预制体
    private GameObject gPreCyliners;//预制体
    private GameObject gPreCapsules;//预制体
    private Track gTrack;

	// Use this for initialization
	void Start () {
        gTrack = new Track();
        //InitMaterailTrack();
        InitFactory();

    }
	
	// Update is called once per frame
	void Update () {

            Load_UnLoad_Move();
    }

    /// <summary>
    /// 小车进行移动
    /// </summary>
    private void Load_UnLoad_Move()
    {
        int pCarCurrentIndex = gAGVBase.GetCurrentIndex();
        int pCarTarget1Index = gAGVBase.GetTarget1Index();
        int pCarTarget2Index = gAGVBase.GetTarget2Index();
        List<Vector3> pCarTrack;
        List<int> pCarTrackList;

        if (gAGVBase.IsFree == false)//AGV是否有任务
        {
            if (gAGVBase.IsStop == false)//AGV有任务并且处于移动状态，则不执行下面
            {
                return;
            }
            else//AGV有任务并且处于停止状态，则不是装货就是卸货
            {
                //1、原材料区域装货
                if (pCarCurrentIndex == 0)
                {
                    //原材料区域装货
                    GameObject pOrigin = gOriginMaterialManage.GetOriginMaterial();
                    gAGVBase.AddMaterial(pOrigin);
                    //小车执行送料
                    gTrack.Search(pCarCurrentIndex, pCarTarget2Index, out pCarTrack, out pCarTrackList);
                    gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                    return;
                }
                //2、机床1处装货
                else if (pCarCurrentIndex == 1)
                {
                    if (gMachineBase1.IsNeedMaterial == false)
                    {
                        //机床1处装货
                        GameObject pOrigin = gMachineBase1.GetFinishMaterial();
                        gAGVBase.AddMaterial(pOrigin);
                        //小车执行送料
                        gTrack.Search(pCarCurrentIndex, pCarTarget2Index, out pCarTrack, out pCarTrackList);
                        gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                        return;
                    }
                    else
                    {
                        GameObject pMaterial = gAGVBase.ReturnMaterial();
                        gMachineBase1.GetProcessMaterial(pMaterial);
                        gAGVBase.IsFree = true;
                        return;
                    }
                }
                //3、机床2处装货
                else if (pCarCurrentIndex == 2)
                {
                    if (gMachineBase2.IsNeedMaterial == false)
                    {
                        //机床2处装货
                        GameObject pOrigin = gMachineBase2.GetFinishMaterial();
                        gAGVBase.AddMaterial(pOrigin);
                        //小车执行送料
                        gTrack.Search(pCarCurrentIndex, pCarTarget2Index, out pCarTrack, out pCarTrackList);
                        gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                        return;
                    }
                    else
                    {
                        GameObject pMaterial = gAGVBase.ReturnMaterial();
                        gMachineBase2.GetProcessMaterial(pMaterial);
                        gAGVBase.IsFree = true;
                        return;
                    }
                }
                //3、机床3处装货
                else if (pCarCurrentIndex == 6)
                {
                    if (gMachineBase3.IsNeedMaterial == false)
                    {
                        //机床3处装货
                        GameObject pOrigin = gMachineBase3.GetFinishMaterial();
                        gAGVBase.AddMaterial(pOrigin);
                        //小车执行送料
                        gTrack.Search(pCarCurrentIndex, pCarTarget2Index, out pCarTrack, out pCarTrackList);
                        gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                        return;
                    }
                    else
                    {
                        GameObject pMaterial = gAGVBase.ReturnMaterial();
                        gMachineBase3.GetProcessMaterial(pMaterial);
                        gAGVBase.IsFree = true;
                        return;
                    }
                }
                //3、机床4处装货
                else if (pCarCurrentIndex == 5)
                {
                    if (gMachineBase4.IsNeedMaterial == false)
                    {
                        //机床4处装货
                        GameObject pOrigin = gMachineBase4.GetFinishMaterial();
                        gAGVBase.AddMaterial(pOrigin);
                        //小车执行送料
                        gTrack.Search(pCarCurrentIndex, pCarTarget2Index, out pCarTrack, out pCarTrackList);
                        gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                        return;
                    }
                    else
                    {
                        GameObject pMaterial = gAGVBase.ReturnMaterial();
                        gMachineBase4.GetProcessMaterial(pMaterial);
                        gAGVBase.IsFree = true;
                        return;
                    }
                }
                else if (pCarCurrentIndex == 3)//在成品仓卸货
                {
                    GameObject pFinishMaterial = gAGVBase.ReturnMaterial();
                    gFinishMaterailManage.AddFinishMaterial(pFinishMaterial);
                    gAGVBase.IsFree = true;
                    return;
                }
            }
        }
        else//AGV没有任务,则开始接受任务
        {
            //判断是否已经完成，如果完成优先送往成品库
            if (gMachineBase2.IsFinish == true)
            {
                //小车移动到machine2即可
                pCarTarget1Index = 2;
                pCarTarget2Index = 3;

                gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                gAGVBase.IsFree = false;
                return;
            }
            if (gMachineBase4.IsFinish == true)
            {
                //小车移动到machine4即可
                pCarTarget1Index = 5;
                pCarTarget2Index = 3;

                gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                gAGVBase.IsFree = false;
                return;
            }
            if (gMachineBase1.IsFinish == true)
            {
                if (gMachineBase2.IsNeedMaterial)//判断是否需要材料，如果需要，则延后执行
                {
                    //小车移动到machine4即可
                    pCarTarget1Index = 1;
                    pCarTarget2Index = 2;

                    gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                    gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                    gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                    gAGVBase.IsFree = false;
                    return;
                }
                else if (gMachineBase4.IsNeedMaterial)
                {
                    //小车移动到machine4即可
                    pCarTarget1Index = 1;
                    pCarTarget2Index = 5;

                    gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                    gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                    gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                    gAGVBase.IsFree = false;
                    return;
                }
            }
            if (gMachineBase3.IsFinish == true)
            {
                if (gMachineBase2.IsNeedMaterial)//判断是否需要材料，如果需要，则延后执行
                {
                    //小车移动到machine4即可
                    pCarTarget1Index = 6;
                    pCarTarget2Index = 2;

                    gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                    gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                    gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                    gAGVBase.IsFree = false;
                    return;
                }
                else if (gMachineBase4.IsNeedMaterial)
                {
                    //小车移动到machine4即可
                    pCarTarget1Index = 6;
                    pCarTarget2Index = 5;

                    gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                    gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                    gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                    gAGVBase.IsFree = false;
                    return;
                }
            }
            if (gMachineBase1.IsNeedMaterial)//判断是否需要材料，如果需要，则延后执行
            {
                //小车移动到machine4即可
                pCarTarget1Index = 0;
                pCarTarget2Index = 1;

                gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                gAGVBase.IsFree = false;
                return;
            }
            //判断是否需要材料，如果需要，则优先执行
            if (gMachineBase3.IsNeedMaterial)
            {
                //小车移动到machine3即可
                pCarTarget1Index = 0;
                pCarTarget2Index = 6;

                gTrack.Search(pCarCurrentIndex, pCarTarget1Index, out pCarTrack, out pCarTrackList);
                gAGVBase.SetTargetAndAcceptIndex(pCarCurrentIndex, pCarTarget1Index, pCarTarget2Index);
                gAGVBase.SetCarTrack(pCarTrack, pCarTrackList);
                gAGVBase.IsFree = false;
                return;
            }
        }      
    }


    /// <summary>
    /// 初始化工厂布局
    /// </summary>
    private void InitFactory()
    {
        //初始化设备位置
        Vector3 pMachinePosition1 =new Vector3(-150f, 64f,-250f);
        Vector3 pMachinePosition2 = new Vector3(150f, 64f, -250f);
        Vector3 pMachinePosition3 = new Vector3(150f, 64f, 250f);
        Vector3 pMachinePosition4 = new Vector3(-150f, 64f, 250f);

        Transform pMachine1 = GameObject.Find("Machine1").transform;
        pMachine1.position = pMachinePosition1;//该功能实现物体的位置初始化定义
        Transform pMachine2 = GameObject.Find("Machine2").transform;
        pMachine2.position = pMachinePosition2;//该功能实现物体的位置初始化定义
        Transform pMachine3 = GameObject.Find("Machine3").transform;
        pMachine3.position = pMachinePosition3;//该功能实现物体的位置初始化定义
        Transform pMachine4 = GameObject.Find("Machine4").transform;
        pMachine4.position = pMachinePosition4;//该功能实现物体的位置初始化定义

        //初始化AGV小车位置
        gAGVBase = GameObject.Find("AGV").GetComponent<AGVBase>();
        gAGVTransform = GameObject.Find("AGV").transform;
        Vector3 pAGVPosition1 = new Vector3(-200f, 15.4f, -150f);
        gAGVTransform.position = pAGVPosition1;//该功能实现物体的位置初始化定义
        //初始化类
        gPreCubes =(GameObject)Resources.Load("Cubes");
        gPreCyliners = (GameObject)Resources.Load("Cylinders");
        gPreCapsules = (GameObject)Resources.Load("Capsules");

        gMachineBase1 = GameObject.Find("Machine1").GetComponent<MachineBase>();
        gMachineBase2 = GameObject.Find("Machine2").GetComponent<MachineBase>();
        gMachineBase3 = GameObject.Find("Machine3").GetComponent<MachineBase>();
        gMachineBase4 = GameObject.Find("Machine4").GetComponent<MachineBase>();

        float pProcess1 = 30.0f;
        float pProcess2 = 45.0f;

        Vector3 pMachine1InputPosition = new Vector3(-250f,35, -200f);
        Vector3 pMachine1OutputPosition = new Vector3(-50f,35, -200f);
        gMachineBase1.InitMachine(pMachine1InputPosition, pMachine1OutputPosition, gPreCubes, gPreCyliners, pProcess1);

        Vector3 pMachine2InputPosition = new Vector3(50f, 35, -200f);
        Vector3 pMachine2OutputPosition = new Vector3(250f,35, -200f);
        gMachineBase2.InitMachine(pMachine2InputPosition, pMachine2OutputPosition, gPreCyliners, gPreCapsules, pProcess2);

        Vector3 pMachine3InputPosition = new Vector3(-250f, 35, 200f);
        Vector3 pMachine3OutputPosition = new Vector3(-50f, 35, 200f);
        gMachineBase3.InitMachine(pMachine3InputPosition, pMachine3OutputPosition, gPreCubes, gPreCyliners, pProcess1);

        Vector3 pMachine4InputPosition = new Vector3(50f, 35, 200f);
        Vector3 pMachine4OutputPosition = new Vector3(250f, 35, 200f);
        gMachineBase4.InitMachine(pMachine4InputPosition, pMachine4OutputPosition, gPreCyliners, gPreCapsules, pProcess2);

        //实例化
        gFinishMaterailManage = new OutPutMaterialManage();
        gOriginMaterialManage = new InPutMaterialManage(gPreCubes);
    }


}
